package com.hw.protocol.tool.decode.hb;

import com.hw.protocol.tool.model.entity.hb.Alertor;
import com.hw.protocol.tool.model.entity.hb.AlertorData;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author : lihy
 * @date : 2023/11/3 10:55
 * description: 报警器实时数据解码器
 */
public class AlertorDecode {

    private ByteBuf dataBuf;

    private Alertor alertor;

    public AlertorDecode(String devSerial, byte[] data) {
        this.dataBuf = Unpooled.wrappedBuffer(data);
        this.alertor = new Alertor();
        alertor.setDevSerial(devSerial);
    }

    public Alertor buildObject(){
        alertor.setTime(DecodeUtil.getTime(dataBuf));
        AlertorData[] alerts = new AlertorData[8];
        for (int i = 0; i < 8; i++) {
            AlertorData alert = new AlertorData();
            alert.setSenser(dataBuf.readByte());
            alert.setData(dataBuf.readFloatLE());
            alert.setStatus(dataBuf.readByte());
            //alert.setType(dataBuf.readByte());
            alerts[i] = alert;
        }
        alertor.setAlerts(alerts);
        alertor.setSignal(dataBuf.readByte());
        alertor.setUploadType(dataBuf.readByte());

        dataBuf.release();
        return alertor;
    }
}
